package com.icbc.open.chengdu.demo.settlement.holdunhold;

import com.csii.pp.icbc.util.MD5Util;
import com.csii.pp.icbc.util.Util;
import com.icbc.api.DefaultIcbcClient;
import com.icbc.api.internal.util.internal.util.fastjson.JSON;
import com.icbc.api.request.SettlementAccountUnholdRequestV1;
import com.icbc.api.response.SettlementAccountUnholdResponseV1;
import com.icbc.open.chengdu.demo.BaseData;
import com.icbc.open.chengdu.demo.utils.GenerateKeyUtils;
import com.icbc.open.chengdu.demo.utils.SecretUtils;

/**
 * 解保留的时候，金额，remarks需要与保留时传递的参数一致，holdDate必须与保留时返回的workdate一致，hold_type必须与保留时的原保留种类一致
 *
 * @author xcz
 */
public class SettlementAccountUnholdTest {

    public static void main(String[] args) throws Exception {
        String sm4Key= GenerateKeyUtils.generateSM4Key();//生成SM4密钥
        DefaultIcbcClient client = new DefaultIcbcClient(BaseData.APP_ID, BaseData.MY_RSA_PRIVATE_KEY, BaseData.ICBC_RSA_PUBLIC_KEY);
        SettlementAccountUnholdRequestV1 request = new SettlementAccountUnholdRequestV1();
        SettlementAccountUnholdRequestV1.SettlementAccountUnholdRequestV1Biz bizContent = new SettlementAccountUnholdRequestV1.SettlementAccountUnholdRequestV1Biz();
        request.setServiceUrl(BaseData.ACCOUNT_UNHOLD_URL);//API地址
        bizContent.setCorpNo(BaseData.APP_ID); //合作方机构编号
        bizContent.setTrxAccDate(BaseData.TEST_TRX_ACC_DATE);//合作方交易日期
        bizContent.setTrxAccTime(BaseData.TEST_TRX_ACC_TIME);//合作方交易时间
        bizContent.setCorpSerno(Util.getNonceStr(16)); //合作方交易单号
        bizContent.setCorpDate(BaseData.TEST_CORP_DATE); //合作方工作日期
        bizContent.setOutServiceCode("accountunhold"); //外部服务代码
        bizContent.setMediumId(SecretUtils.encryptSM4(sm4Key, BaseData.TEST_ACCOUNT_NUMBER_2)); //工行联名卡号
        bizContent.setCcy(1); //币种
        bizContent.setCashExFlag(9); //钞汇标志
        bizContent.setAmount(BaseData.TEST_HOLD_AMOUNT); //交易额300元 与单笔保留的金额一致

        bizContent.setHoldType(BaseData.TEST_HOLD_TYPE); //保留种类
        bizContent.setHoldDate("2019-04-25");//原认筹保留日期 保留时，返回的数据
        bizContent.setSummary("资金解除保留"); //摘要
        bizContent.setRemarks(BaseData.TEST_HOLD_REMARKS); //注释  需要和保留时的字段一致
        bizContent.setSecretKey(SecretUtils.encryptSM2(sm4Key));//SM4密钥
        bizContent.setMediumIdHash(MD5Util.string2MD5(BaseData.TEST_ACCOUNT_NUMBER_2));//卡号的哈希值
        request.setBizContent(bizContent);
        SettlementAccountUnholdResponseV1 response = client.execute(request);
        System.out.println(JSON.toJSONString(response));
        if (response.isSuccess() && response.getReturnCode() == 0) {
            // 业务成功处理
            System.out.println("实际余额："+response.getAccountBalance());
            System.out.println("可用余额："+response.getAccountUseamt());
        } else {
            // 失败
        }


    }
}
